{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```py\n",
    "# -*- conding:utf-8 -*-\n",
    "\n",
    "import logging\n",
    "import logging.config\n",
    "import os\n",
    "\n",
    "__all__ = ['getLogger']\n",
    "\n",
    "DIR_DEBUG = './logs/debug'\n",
    "DIR_INFO = './logs/info'\n",
    "DIR_ERROR = './logs/error'\n",
    "\n",
    "os.makedirs(DIR_DEBUG, exist_ok=True)\n",
    "os.makedirs(DIR_INFO, exist_ok=True)\n",
    "os.makedirs(DIR_ERROR, exist_ok=True)\n",
    "\n",
    "IS_INIT = False\n",
    "\n",
    "LOGGING = {\n",
    "    \"version\": 1,\n",
    "    \"disable_existing_loggers\": False,\n",
    "    # 日志输出格式设置\n",
    "    \"formatters\": {\n",
    "        \"simple\": {\n",
    "            'format': '%(asctime)s [%(name)s:%(lineno)d] [%(levelname)s]- %(message)s'\n",
    "        },\n",
    "        'standard': {\n",
    "            'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(levelname)s]- %(message)s'\n",
    "        },\n",
    "    },\n",
    "\n",
    "    \"handlers\": {\n",
    "        # 控制台处理器,控制台输出\n",
    "        \"console\": {\n",
    "            \"class\": \"logging.StreamHandler\",\n",
    "            \"level\": \"DEBUG\",\n",
    "            \"formatter\": \"simple\",\n",
    "            \"stream\": \"ext://sys.stdout\"\n",
    "        },\n",
    "        # 默认处理器, 文件大小滚动\n",
    "        \"debug\": {\n",
    "            \"class\": \"logging.handlers.RotatingFileHandler\",\n",
    "            \"level\": \"DEBUG\",\n",
    "            \"formatter\": \"simple\",\n",
    "            'filename': os.path.join(DIR_DEBUG, 'debug.log'),\n",
    "            \"maxBytes\": 1024 * 1024 * 5,  # 5 MB\n",
    "            \"backupCount\": 50,\n",
    "            \"encoding\": \"utf8\"\n",
    "        },\n",
    "        # 错误日志, 文件大小滚动\n",
    "        'error': {\n",
    "            'level': 'ERROR',\n",
    "            'class': 'logging.handlers.RotatingFileHandler',\n",
    "            'filename': os.path.join(DIR_ERROR, 'error.log'),\n",
    "            'maxBytes': 1024 * 1024 * 2,\n",
    "            'backupCount': 10,\n",
    "            'formatter': 'standard',\n",
    "            'encoding': 'utf-8',\n",
    "        },\n",
    "        # info 及以上级别, 日期滚动\n",
    "        'info': {\n",
    "            'class': 'logging.handlers.TimedRotatingFileHandler',\n",
    "            'level': 'INFO',\n",
    "            \"formatter\": \"simple\",\n",
    "            'filename': os.path.join(DIR_INFO, 'info.log'),\n",
    "            'when': 'D',  # 单位： 天\n",
    "            'interval': 1,  # 滚动周期\n",
    "            'backupCount': 30,  # 备份30天\n",
    "            \"encoding\": \"utf8\"\n",
    "        },\n",
    "    },\n",
    "    'loggers': {\n",
    "        'websockets': {\n",
    "            'handlers': ['debug', 'console'],\n",
    "            'level': \"ERROR\",\n",
    "            'propagate': False\n",
    "        },\n",
    "    },\n",
    "    \"root\": {\n",
    "        'handlers': ['debug', 'error', 'info', 'console'],\n",
    "        'level': \"DEBUG\",\n",
    "        'propagate': False\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "def initLogConf():\n",
    "    \"\"\"\n",
    "    配置日志\n",
    "    \"\"\"\n",
    "    global IS_INIT\n",
    "    if IS_INIT:\n",
    "        return\n",
    "    logging.config.dictConfig(LOGGING)\n",
    "    logging.debug(\"Init logging\")\n",
    "    IS_INIT = True\n",
    "\n",
    "\n",
    "def getLogger(name):\n",
    "    return logging.getLogger(name)\n",
    "\n",
    "\n",
    "initLogConf()\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    logger = getLogger('aaa')\n",
    "    logger.debug('debug')\n",
    "    logger.info('info')\n",
    "    logger.error('error')\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
